package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ItCompany;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ItTopic;
import com.ruoyi.system.service.IItTopicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 话题Controller
 *
 * @author ruoyi
 * @date 2022-01-26
 */
@RestController
@RequestMapping("/mini/topic")
public class ItTopicController extends BaseController {
    @Autowired
    private IItTopicService itTopicService;

    /**
     * 查询话题列表
     */
    @PreAuthorize("@ss.hasPermi('mini:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItTopic itTopic) {
        startPage();
        List<ItTopic> list = itTopicService.selectItTopicList(itTopic);
        return getDataTable(list);
    }

    /**
     * 查询热门话题列表
     *
     * @return
     */
    @GetMapping("/listget")
    public TableDataInfo listget() {
        //查询话题根据关联查询出热门话题
        List<Map<Object, Object>> list = itTopicService.selectItTopicListCount();
        return getDataTable(list);
    }


    /**
     * 导出话题列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:export')")
    @Log(title = "话题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItTopic itTopic) {
        List<ItTopic> list = itTopicService.selectItTopicList(itTopic);
        ExcelUtil<ItTopic> util = new ExcelUtil<ItTopic>(ItTopic.class);
        util.exportExcel(response, list, "话题数据");
    }

    /**
     * 获取话题详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:topic:query')")
    @GetMapping(value = "/{itId}")
    public AjaxResult getInfo(@PathVariable("itId") String itId) {
        return AjaxResult.success(itTopicService.selectItTopicByItId(itId));
    }

    /**
     * 新增话题
     */
    @PreAuthorize("@ss.hasPermi('system:topic:add')")
    @Log(title = "话题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItTopic itTopic) {
        itTopic.setCreateTime(new Date());
        return toAjax(itTopicService.insertItTopic(itTopic));
    }

    /**
     * 修改话题
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "话题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItTopic itTopic) {
        return toAjax(itTopicService.updateItTopic(itTopic));
    }

    /**
     * 删除话题
     */
    @PreAuthorize("@ss.hasPermi('system:topic:remove')")
    @Log(title = "话题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itIds}")
    public AjaxResult remove(@PathVariable String[] itIds) {
        return toAjax(itTopicService.deleteItTopicByItIds(itIds));
    }
}
